package game;
import interfaces.IPlayer;

import java.util.ArrayList;

import ki.NimKI;

import player.NimKIPlayer;
import player.Player;


public class NimGame {
	int sticks; //holds the number of sticks at the table
	int maxRemove; //maximum number of stick that can be removed from the pile
	
	int round = 0; //round of the game
	int turn = 0; //who is next, 0 = player1, 1 = player 2
	
	ArrayList<IPlayer> players = new ArrayList<IPlayer>();
	
	public NimGame(int sticks, int maxRemove) {
		this.sticks = sticks;
		this.maxRemove = maxRemove;
		
	}
	
	//start the game	
	public void start() {
		
		while (this.sticks > 0) {
			this.round ++;
			
			this.printGameStats();
			
			
			//assign a player to take his turn. he will return a number of sticks to remove from the table. when the player
			//wants to remove more sticks than maxSticksToRemove (or 0) he will be ask to remove another count of sticks
			int sticksToRemove;
			int maxSticksToRemove = getMaxSticksToRemove();
			
			while((sticksToRemove = this.players.get(this.turn).turn())  > maxSticksToRemove || sticksToRemove <= 0) {
				this.sendMessage(this.players.get(this.turn), "you have to remove at least 1 stick and less or equal " + maxSticksToRemove + " sticks");
				
			} 
			
			//remove sticks from table
			this.removeSticks(sticksToRemove);
			
			//toggle to next player
			this.nextPlayer();
			
		}
		
		this.printFinalStats();
		
	}
	
	
	//returns the number of sticks that can be removed from stack (e.g this.maxRemove or this.sticks)
	public int getMaxSticksToRemove() {
		if (this.sticks < this.maxRemove) {
			return this.sticks;
		} else {
			return this.maxRemove;
		}
		
	}
	
	//removes sticksToRemove sticks from the table
	public void removeSticks(int sticksToRemove) {
		this.sticks -= sticksToRemove;
		//this.sendMessage(this.players.get(this.turn), sticksToRemove + " have been removed from the table");
		
	}
	
	//toggle this.turn to the other player -> next players turn
	public void nextPlayer() {
		if (this.turn == 0) {
			this.turn = 1;
			
		} else {
			this.turn = 0;
			
		}
		
	} 
	
	//adds a player to the game
	public void addPlayer(IPlayer player) {
		if (this.players.size() < 2) {
			this.players.add(player);
			System.out.println("Player added");
			
		} else {
			System.out.println("there are allready two players!");
			
		}
		
	}
	
	//sends a message to the specified player
	private void sendMessage(IPlayer player, String message) {
		player.printMessage(message);
		
	}
	
	//print out game stats (e.g. round, who is next)
	private void printGameStats(){
		System.out.println("=== ROUND " + this.round + " ===");
		System.out.println("sticks on table: " + this.sticks);
		this.printSticks();
		System.out.println("turn: player " + this.players.get(this.turn).getName());
		System.out.println();
	}
	
	//print out the number of sticks (e.g 3 sticks = |||)
	private void printSticks() {
		String sticksOnTable = "";
		for (int i = this.sticks; i > 0; i--) {
			sticksOnTable += "|";
			
		}
		
		System.out.println(sticksOnTable);
	}
	
	//print out the winner of the game and some game stats
	private void printFinalStats() {
		//toggle back to the player, that took the last move
		this.nextPlayer();
		
		System.out.println("=== END OF THE GAME ===");
		System.out.println("the winner is: " + this.players.get(this.turn).getName());
		
	}
	
	public int getSticks() {
		return sticks;
	}

	public int getMaxRemove() {
		return maxRemove;
	}

	public static void main(String[] args) {
		NimGame game = new NimGame(201, 3);
		
		//ki first
//		game.addPlayer(new NimKIPlayer("NimKI", new NimKI(game)));
//		game.addPlayer(new Player("Spieler"));
		
		//player first
		game.addPlayer(new NimKIPlayer("NimKI", new NimKI(game)));
		game.addPlayer(new Player("Spieler"));
		
		//game.addPlayer(new NimKIPlayer("NimKI 2", new NimKI(game)));	
		
		

		
		game.start();
		
	}
	
}
